#include "LitStdAfx.h"
#include "LitTokeniser.h"

namespace Lit
{
	Tokeniser::Tokeniser( ){ }
	Tokeniser::Tokeniser( const String &str, const String &delim ){ if( !str.empty() ) vec = str.tokenise(delim.str()); }
	Tokeniser::Tokeniser( const Tokeniser& rk ){ vec=rk.vec; }
	Tokeniser::~Tokeniser( ){}

	String Tokeniser::join( const String &link ) const
	{
		String ret;
		for( auto i=vec.begin();
			i!=vec.end();
			i++ )
		{
			ret += (*i)+link;
		}
		ret.trim();
		return ret;
	}
		
	Tokeniser& Tokeniser::operator=( const Tokeniser& rk ){ vec=rk.vec; return *this; }
	Tokeniser& Tokeniser::operator+=( const Tokeniser& rk ){ for( auto i=rk.vec.begin(); i!=rk.vec.end(); i++ ) vec.push_back( String((*i)) ); return *this; }
	Tokeniser Tokeniser::operator+( const Tokeniser& rk ) const{ Tokeniser tok; for( auto i=rk.vec.begin(); i!=rk.vec.end(); i++ ) tok.vec.push_back( String((*i)) ); return tok; }

	String Tokeniser::argAt( size_t id )
	{
		if( vec.empty() ) throw MissingParameter( "arg("+String(id).str()+")", EXC_LOC );
		if( id >= vec.size() ) throw std::range_error( String()(id)(" not in range [0,")(vec.size())("]").str() );
		return vec[id];
	}
	String Tokeniser::nextArg(){
		if( vec.empty() ) throw MissingParameter( "nextArg", EXC_LOC );
		String a = *vec.begin();
		vec.erase( vec.begin() );
		return a;
	}
	bool Tokeniser::hasMoreArgs() const{
		return !vec.empty();
	}
	String Tokeniser::rest() const{ return join(); }
	size_t Tokeniser::count() const{ return vec.size(); }
	bool Tokeniser::empty() const{ return vec.empty(); }
};